<template>
  <span>{{ keysName }}</span>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, toRefs, watch } from 'vue'
import { userModule } from '@/common/store/modules/user'
export default defineComponent({
  name: 'satisComponent',
  components: {},
  props: {
    callType: {
      // 当前通话记录的通话类型
      type: String,
      default: 'dialout'
    },
    keys: {
      // 当前通话记录的满意度
      type: Array,
      default: () => {
        return []
      }
    },
    satisfactionData: {
      // 满意度配置
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  setup(props) {
    // 获取满意度的配置
    const USERMODULE = userModule()
    const statiNormalMap: any = {
      2: {
        '1': '满意',
        '2': '不满意'
      },
      3: {
        '1': '满意',
        '2': '一般',
        '3': '不满意'
      },
      4: {
        '1': '非常满意',
        '2': '满意',
        '3': '一般',
        '4': '不满意'
      },
      5: {
        '1': '非常满意',
        '2': '满意',
        '3': '一般',
        '4': '不满意',
        '5': '非常不满意'
      }
    }
    const STATE = reactive({
      keysName: '--'
    })
    const handleStatis = () => {
      let satisMap: any = {}
      const satisObj: any = props.satisfactionData || {}
      let satiConfig: any = {}
      // 外呼的话，取外呼满意度配置 反之取 呼入满意度配置
      satiConfig = ['dialout', 'previewDialout'].includes(props.callType) ? satisObj.outConfig : satisObj.inConfig
      let str = ''
      props.keys.forEach((item1: any) => {
        if (item1.indexOf('_') > -1) {
          // 用下划线分割的，说明是二级满意度
          const keysArr = item1.split('_') || []
          const secondSati = satiConfig.secondSatisfactions || []
          secondSati.forEach((secondSatiItem: any) => {
            if (keysArr[0] === secondSatiItem.firstSatisfaction) {
              str += `${secondSatiItem.firstSatisfactionLabel}/`
              const secondKeys = secondSatiItem.keys || []
              secondKeys.forEach((keyItem: any) => {
                if (keysArr[1] === keyItem.key) {
                  str += `${keyItem.label}/`
                }
              })
            }
          })
        } else {
          // 默认满意度，根据选择的配置项从map中取出满意度固定列表
          satisMap = statiNormalMap[satiConfig.evaluationModel] || {}
          if (satisMap[item1]) {
            str += `${satisMap[item1]}/`
          }
        }
      })
      str = str ? str.substring(0, str.length - 1) : '--'
      STATE.keysName = str
    }
    watch(
      () => props.keys,
      () => {
        setTimeout(() => {
          if (Object.keys(props.satisfactionData).length <= 0) {
            // 没有取回配置的情况下，不执行
            return false
          }
          handleStatis()
        }, 1000)
      },
      { immediate: true }
    )
    return { ...toRefs(STATE) }
  }
})
</script>

<style lang="stylus" scoped></style>
